package com.yeyks.service.impl;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.appcommon.dto.account.AppGetAccountDTO;
import com.yeyks.common.appcommon.param.account.AddAccountParam;
import com.yeyks.common.appcommon.param.account.GetAccountEditParam;
import com.yeyks.common.appcommon.param.account.UpdateAccountParam;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.dao.AppAccountInfoMapper;
import com.yeyks.entity.AppAccountInfo;
import com.yeyks.service.AppAccountInfoService;
import com.yeyks.service.AppAccountTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class AppAccountInfoServiceImpl extends AbstractAppCommon implements AppAccountInfoService {

    @Autowired
    private AppAccountInfoMapper accountInfoMapper;

    @Autowired
    private AccountInfoService accountInfoService;

    @Autowired
    private AppAccountTokenService appAccountTokenService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult addAccount(AddAccountParam param) {

        //验证token是否有效
        CommonResult<AppAccountInfo> commonResult = appAccountTokenService.getAvailAccount(param.getToken(),
                param.getVersion(), param.getPlatform());
        if (null == commonResult.getData()) {
            return commonResult;
        }
        //校验参数，可以扩展
        CommonResult paramResult = this.validAddAccountParam(param);
        if (paramResult.getCode() == CommonResult.FAILED) {
            return paramResult;
        }
        AppAccountInfo accountInfo = commonResult.getData();
        AccountInfo addAccountInfo = MyBeanUtils.convert(param, AccountInfo.class);
        //补全参数
        this.convertAccountInfo(addAccountInfo, accountInfo, param.getDivCode());

        try {
            accountInfoService.saveUser(addAccountInfo);
            return CommonResult.success(null, "添加代理信息成功");
        }catch (Exception e){
            throw new OftenFiledException("添加数据失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateAccount(UpdateAccountParam param) {

        //验证token是否有效
        CommonResult<AppAccountInfo> commonResult = appAccountTokenService.getAvailAccount(param.getToken(),
                param.getVersion(), param.getPlatform());
        if (null == commonResult.getData()) {
            return commonResult;
        }
        //校验更新参数，可以扩展
        CommonResult paramResult = this.validAddAccountParam(MyBeanUtils.convert(param, AddAccountParam.class));
        if (paramResult.getCode() == CommonResult.FAILED) {
            return paramResult;
        }
        AppAccountInfo accountInfo = commonResult.getData();
        AccountInfo updateAccountInfo = MyBeanUtils.convert(param, AccountInfo.class);
        //补全参数
        this.convertAccountInfo(updateAccountInfo, accountInfo, param.getDivCode());

        try {
            accountInfoService.update(updateAccountInfo);
            return CommonResult.success(null, "更新代理信息成功");
        }catch (Exception e){
            throw new OftenFiledException("更新数据失败");
        }
    }

    @Override
    public CommonResult getAccountEdit(GetAccountEditParam param) {

        //验证token是否有效
        CommonResult<AppAccountInfo> commonResult = appAccountTokenService.getAvailAccount(param.getToken(), param.getVersion(), param.getPlatform());
        if (null == commonResult.getData()) {
            return commonResult;
        }

        AppAccountInfo accountInfo = accountInfoMapper.selectByPrimaryKey(param.getId());
        if (null != accountInfo) {

            AppGetAccountDTO appGetAccountDTO = MyBeanUtils.convert(accountInfo, AppGetAccountDTO.class);
            //得到最后的code
            if (!StringUtils.isEmpty(accountInfo.getCountryCode())) {
                appGetAccountDTO.setDivCode(accountInfo.getCountryCode());
            } else if (!StringUtils.isEmpty(accountInfo.getCityCode())) {
                appGetAccountDTO.setDivCode(accountInfo.getCityCode());
            } else if (!StringUtils.isEmpty(accountInfo.getProvinceCode())) {
                appGetAccountDTO.setDivCode(accountInfo.getProvinceCode());
            }
            //返回确认密码
            appGetAccountDTO.setConfirmPwd(accountInfo.getPassword());

            return CommonResult.success(appGetAccountDTO);
        }
        return CommonResult.failed("该账户不存在");
    }

    /**
     * 添加账号的密码校验 == 》 也可以是其他参数的校验
     *
     * @param param 输入参数
     * @return 返回结果
     */
    private CommonResult validAddAccountParam(AddAccountParam param) {

        if (!param.getPassword().equals(param.getConfirmPwd())) {
            return CommonResult.failed("确认密码和设置密码不一致");
        }

        return CommonResult.success(null, "参数验证通过");
    }
}
